#include "read_table.h"
#include "encoder_app.h"
#include "config.h"
#include <string.h>
#include <math.h>

Uart_Read_Table_t Uart_Read_Table = 
{
  .time_count = 0
};

/*********************************************************************************
�������� �� Table_Para_Init
��    �� �� ��������ʼ��
��    �� �� ��
�� �� ֵ �� ��
˵    �� �� ��
**********************************************************************************/
void Table_Para_Init(void)
{
  for(uint8_t i=0;i<12;i++)
  {
    Uart_Read_Table.read_num[i] = 0x30;
    Uart_Read_Table.sn_num[i] = 0x30;
  }
}

/*********************************************************************************
�������� �� Send_Read_Table_Cmd
��    �� �� ���Ͷ�ȡ����������
��    �� �� ��
�� �� ֵ �� ��
˵    �� �� ��
**********************************************************************************/
void Send_Read_Table_Cmd(void)
{
  Uart_Read_Table.tx_buffer[0] = 0x68;
  Uart_Read_Table.tx_buffer[1] = 0x10;
  Uart_Read_Table.tx_buffer[2] = 0xFF;
  Uart_Read_Table.tx_buffer[3] = 0xFF;
  Uart_Read_Table.tx_buffer[4] = 0xFF;
  Uart_Read_Table.tx_buffer[5] = 0xFF;
  Uart_Read_Table.tx_buffer[6] = 0xFF;
  Uart_Read_Table.tx_buffer[7] = 0xFF;
  Uart_Read_Table.tx_buffer[8] = 0xFF;
  Uart_Read_Table.tx_buffer[9] = 0x01;
  Uart_Read_Table.tx_buffer[10] = 0x03;
  Uart_Read_Table.tx_buffer[11] = 0x90;
  Uart_Read_Table.tx_buffer[12] = 0x1F;
  Uart_Read_Table.tx_buffer[13] = 0x00;
  Uart_Read_Table.tx_buffer[14] = 0x24;
  Uart_Read_Table.tx_buffer[15] = 0x16;
  
  Uart0_Send_Data(Uart_Read_Table.tx_buffer, 16);
} 

/*********************************************************************************
�������� �� Total_Flow_Unit_Convert
��    �� �� �ۼ�������λת��
��    �� �� *p_read_table������ָ�룬unit����λ
�� �� ֵ �� ��
˵    �� �� ��
**********************************************************************************/
void Total_Flow_Unit_Convert(Uart_Read_Table_t *p_read_table, uint8_t unit)
{
  uint8_t i;
  float total_flow = 0;
  
  //��λת��  
  switch(unit)
  {
    case CONFIG_UNIT_M3: total_flow = (float)p_read_table->total_flow_L / 1000;break;//����ת����������
    case CONFIG_UNIT_01M3: total_flow = (float)p_read_table->total_flow_L / 1000 * 10;break;//����ת����0.1������
    case CONFIG_UNIT_001M3: total_flow = (float)p_read_table->total_flow_L / 1000 * 100;break;//����ת����0.01������
    case CONFIG_UNIT_0001M3: total_flow = p_read_table->total_flow_L;break;//����ת����0.001������
    case CONFIG_UNIT_CF: total_flow = (float)p_read_table->total_flow_L / L_TO_CF_RATIO;break;//����ת��������Ӣ��
    case CONFIG_UNIT_10CF: total_flow = (float)p_read_table->total_flow_L / L_TO_CF_RATIO / 10;break;//����ת����10����Ӣ��
    case CONFIG_UNIT_01GAL: total_flow = (float)p_read_table->total_flow_L / L_TO_USGAL_RATIO * 10;break;//����ת����0.1����
    case CONFIG_UNIT_GAL: total_flow = (float)p_read_table->total_flow_L / L_TO_USGAL_RATIO;break;//����ת���ɼ���
    case CONFIG_UNIT_10GAL: total_flow = (float)p_read_table->total_flow_L / L_TO_USGAL_RATIO / 10;break;//����ת����10����
    default:break;
  }
  
  for(i=0;i<8;i++)
  {
    p_read_table->read_num[i] = (unsigned long)(total_flow/pow(10,(7-i))) % 10 + 0x30;
  }
}

/*********************************************************************************
�������� �� Table_Data_Convert
��    �� �� ������ת��
��    �� �� *p_read_table������ָ�룬*p_data�����ڽ�������
�� �� ֵ �� ��
˵    �� �� ��
**********************************************************************************/
void Table_Data_Convert(Uart_Read_Table_t *p_read_table, Uart_Data_t *p_data)
{
  uint8_t i,j,num[2][12] = {0};
  unsigned long cal_data = 0;
  
  System_Config_t *p_config = &System_Config;
  
  for(i=0;i<2;i++)
  {
    for(j=0;j<4;j++)
    {
      if(i==0)
      {
        num[i][j*2] = (p_data->rx_data[2+j]>>4) & 0x0F; //��4λ
        num[i][(j*2)+1] = p_data->rx_data[2+j] & 0x0F;  //��4λ
      }
      else 
      {
        num[i][j*2] = (p_data->rx_data[17-j]>>4) & 0x0F; //��4λ
        num[i][(j*2)+1] = p_data->rx_data[17-j] & 0x0F;  //��4λ
      }
     
      if(num[i][j*2] > 9)
      {
        if(i==0)
        {
          p_read_table->sn_num[j*2] = num[i][j*2] + 0x37;
        }
      }
      else 
      {
        if(i==0)
        {
          p_read_table->sn_num[j*2] = num[i][j*2] + 0x30;
        }
        else p_read_table->read_num[j*2] = num[i][j*2] + 0x30;
      }
      
      if(num[i][(j*2)+1] > 9)
      {
        if(i==0)
        {
          p_read_table->sn_num[(j*2)+1] = num[i][(j*2)+1] + 0x37;
        }
      }
      else 
      {
        if(i==0)
        {
          p_read_table->sn_num[(j*2)+1] = num[i][(j*2)+1] + 0x30;
        }
        else p_read_table->read_num[(j*2)+1] = num[i][(j*2)+1] + 0x30;
      }
    }
  }
  
  for(i=0;i<8;i++)
  {
    cal_data += (p_read_table->read_num[i] - 0x30) * pow(10,(7-i));//ԭʼ���ݵ�λ��
  }
  
  p_read_table->total_flow_L = cal_data;

  Total_Flow_Unit_Convert(p_read_table, p_config->unit);
}

/*********************************************************************************
�������� �� Analysis_Read_Table_Data
��    �� �� ������������
��    �� �� *p_read_table������ָ�룬*p_buffer�����ڽ��ջ��棬*p_data�����ڽ�������
�� �� ֵ �� true���������ݳɹ���false����������ʧ��
˵    �� �� ��
**********************************************************************************/
bool Analysis_Read_Table_Data(Uart_Read_Table_t *p_read_table, Uart_Buffer_t *p_buffer, Uart_Data_t *p_data)
{
  uint8_t i,head = 0;
  
	while(p_buffer->tx != p_buffer->rx)
	{
		if((p_data->rx_length+1) <= UART_DATA_LENGTH_MAX)
		{
			p_data->rx_data[p_data->rx_length] = p_buffer->buff[p_buffer->tx];
			p_buffer->tx = (p_buffer->tx + 1) & (UART_DATA_LENGTH_MAX-1);
			p_data->rx_length++;
		}
		else p_data->rx_length = 0;
	}

  if(p_data->rx_length < READ_TABLE_DATA_LENGTH) return false;
    
  //���Ұ�ͷ
  for(i=0;i<p_data->rx_length;i++)
  {
    if(p_data->rx_data[i] == READ_TABLE_HEAD)
    {
      head = 1;
      break;//�ҵ���ͷ���˳�ѭ��
    }
  }
  
  if(head != 1)//û�в��ҵ���ͷ
  {
    p_data->rx_length = 0;
    return false;
  }
  
  //ȥ����ͷ֮ǰ����Ч����
  if(i != 0)
  {
    memcpy(p_data->rx_data,&p_data->rx_data[i],p_data->rx_length-i);
    p_data->rx_length = p_data->rx_length - i;
  }
  
  //�жϰ�β
  if(p_data->rx_data[39] != READ_TABLE_TAIL)
  {
    memset(p_data->rx_data, 0, UART_DATA_LENGTH_MAX);//�������
    p_data->rx_length = 0;
    
    return false;
  }
  
  Table_Data_Convert(p_read_table, p_data);//����ת��
  Encoder_Data_Update(&Encoder_Data);//���±�����
    
  memset(p_data->rx_data, 0, UART_DATA_LENGTH_MAX);//�������
  p_data->rx_length = 0;
  
  return true;
}

void test1_demo(void)
{
  
}

